'use strict'

define(['jquery', 'bootstrap', 'ajaxTab', 'select2', 'shiftform', 'validate', 'validate.en'], function($) {
  
  return {
        
    init: function() {
        this.initPage();
        this.create();
        this.delete();
        this.agentPay();
    },
    
    initPage: function(){
        
        $(".ajax-tab").ajaxtab();
    },
    
    create: function() {
    	var that = this;
        $(document).on('shown.bs.modal', '#discount-create', function(e){
        	var $discountType = $('select[name="discountType"');
        	$discountType.change(function() {
        		var type = $(this).val();
            	if(type === 'payment') {
            		$('.payment').removeClass('hide');
            		$('input[name="payment"]').attr("data-validate", "required|discount_validate");
            		$('input[name="commission"]').attr("data-validate", "required|discount_validate")
            		$('.discount').addClass('hide');
            		$('input[name="discount"]').removeAttr("data-validate").val("");
            	} else if(type === 'discount') {
            		$('.discount').removeClass('hide');
            		$('input[name="discount"]').attr("data-validate", "required|number|percentage_validate");
            		$('input[name="commission"]').attr("data-validate", "required|discount_validate|percentage_validate");
            		$('.payment').addClass('hide');
            		$('input[name="payment"]').removeAttr("data-validate").val("");
            	}
        	})
        	
        	var $userDiscountValidate = {'discount_validate' : function(value) { return that.validateDiscount(value); }}
        	$.extend($.bt_validate.fn, $userDiscountValidate)
        	$.extend($.bt_validate.text, {'discount_validate' : '请输入数字格式,中间以英文逗号隔开!'})
        	
        	var $percentageValidate = {'percentage_validate' : function(value) { return that.validatePercentage(value); }}
        	$.extend($.bt_validate.fn, $percentageValidate)
        	$.extend($.bt_validate.text, {'percentage_validate' : '请输入小于100%的数字!'})

	    	$(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
	        
	        var $form = $(e.currentTarget).find("form")                          //bind shiftform
	        $form.shiftform({
	          beforeSubmit : function(el) {
	        	$form.trigger("validate.ajax.submit");
	            return $.bt_validate.result[$.bt_validate.form_id];
	          }
	        })
        })
      },
      
      validateDiscount: function(value) {
    	  var result = true;
    	  
		  if(value != '') {
			  var offers = value.split(',');
    		  for(var j=0; j<offers.length; j++) {
	          	if(isNaN(offers[j])){
	          		result = false;
	          	}
	          }
		  }
          return result;
      },
      
      validatePercentage: function(value) {
    	  var result = true;
    	  
		  if(value != '') {
			  var offers = value.split(',');
    		  for(var j=0; j<offers.length; j++) {
	          	if(offers[j] > 100){
	          		result = false;
	          	}
	          }
		  }
          return result;
      },
      
      delete: function() {
          $(document).on('shown.bs.modal', '#discount-delete', function(e){
          	
        	  $(e.currentTarget).find("form").shiftform() 
              
          })
      },
      
      agentPay: function() {
          $(document).on('shown.bs.modal', '#record-pay', function(e){
          	
        	  $(e.currentTarget).find("form").shiftform({
        		  success: "#withdrawal-ajax:load:refresh"
        	  }) 
              
          })
        }
    
  }
})
